import { Controller, Get, InternalServerErrorException } from '@nestjs/common'
import { AppService } from './app.service'
import { AdminUserService } from 'nestjs-admin'
import { ConfigService } from '@nestjs/config'

@Controller()
export class AppController {
  constructor(
    private readonly appService: AppService,
    private readonly adminUserService: AdminUserService,
    private readonly configService: ConfigService
  ) {}

  @Get('api/hello')
  getHello() {
    return this.appService.getHello()
  }

  @Get('admin/init')
  async initAdmin() {
    // TODO: use another way to init
    const username = this.configService.get('ADMIN_USERNAME')
    const password = this.configService.get('ADMIN_PASSWORD')

    if (!username || !password) {
      throw new InternalServerErrorException()
    }

    // https://github.com/Theodo-UK/nestjs-admin/pull/258
    await this.adminUserService.create(username, password)
    return this.adminUserService.hashPassword(password)
  }
}
